Comparison Operators (for Numerical values)
//FOR char, byte, short, int, long, float, double.-------------------------------------------------
char left = 65;
long right = 70;
if (left == right) { System.out.println("Left equals right."); } // Equal
if (left != right) { System.out.println("Left is different from right."); } // Not equal
if (left < right) { System.out.println("Left is smaller then right."); } // Less than
if (left <= right) { System.out.println("Left is smaller or equals right."); } // Less than or equal
if (left > right) { System.out.println("Left is greater then right."); } // Greater than
if (left >= right) { System.out.println("Left is greater or equals right."); } // Greater than or equal
//FOR boolean.-------------------------------------------------------------------------------------
boolean l = true;
boolean r = false;
if (l == r) { System.out.println("Left equals right."); }
if (l != r) { System.out.println("Left is different from right."); }
Comparison Operators (for Objects and Strings)
//FOR Objects.-------------------------------------------------------------------------------------
String text = "Hello";
if (text instanceof String) { System.out.println("Object is of class String."); }
//FOR Strings.-------------------------------------------------------------------------------------
String text = "Hello";
if (text.equals ("Hello")) { System.out.println("text is equal to \"Hello\"." ); }
if (text.equalsIgnoreCase("hello")) { System.out.println("text is equal to \"hello\"." ); }
if (text.contains ("llo" )) { System.out.println("text contains \"llo\"." ); }
if (text.contentEquals ("Hello")) { System.out.println("text is equal to \"Hello\"." ); }
if (text.startsWith ("He" )) { System.out.println("text starts with \"He\"." ); }
if (text.startsWith ("llo",2)) { System.out.println("text at index 2 starts with \"llo\"."); }
if (text.endsWith ("llo" )) { System.out.println("text ends with \"llo\"." ); }
if (text.matches ("Hello")) { System.out.println("text matches regex \"Hello\"." ); }
Arithmetic Operators (perform mathematical operations)
//TEST VARIABLES.
int x = 10;
int y = 20;
//ARITHMETIC OPERATORS.
int plus = +y; //+10 = +10
int negate = -y; //-10 = -10
int add = x+y; //10+20 = 30
int subtract = x-y; //10-20 = -10
int multiply = x*y; //10*20 = 200
int divide1 = x/y; //10/20 = 0.5 = 0
float divide2 = (float)x/y; //10.0/20.0 = 0.5
int reminder = x%y; //10%20 = 0*20+10 = 10
int increment1 = ++x; //Increment x by 1 and then store x into increment1.
int decrement1 = --x; //Decrement x by 1 and then store x into decrement1.
int increment2 = x++; //Store x into increment2 and then increment x by 1.
int decrement2 = x--; //Store x into decrement2 and then decrement x by 1.